import pandas as pd
import time

from bqtool.tgsdata.kafka_kit import Kafka_producer
from bqtool.utils.tools import get_pg, to_pdtime_str, get_mysql

producer = Kafka_producer(bootstrap_servers=['10.150.27.143:9092'],kafkatopic='passcar')
data = pd.read_csv('../test/data1')
data = data.iloc[0:10000]

client = get_pg()
client2 = get_mysql('root','ehl1234','10.2.111.65','grafana')
data.loc[:, 'PASSTIME'] = to_pdtime_str(data['PASSTIME'], tz=False)
data = data.set_index('PASSTIME')
data.drop(data.columns[0], axis=1, inplace=True)
start = time.clock()

# data.to_sql('t_pass',client,if_exists='append',chunksize=2* 10 ** 2)
# end = time.clock()
# print(f'now{end-start}')
# data.to_sql('t_pass_c',client,if_exists='append')
data.to_sql('t_pass',client2,if_exists='append')
# print(f'now{time.clock()-end}')

data_len = len(data)
count = int(len(data)/100)
count = count if data_len%100==0 else count+1
for i in range(count):
    message = data.iloc[i*100:(i+1)*100]
    producer.send_df(data.iloc[0:100])
# data.apply(producer.send_df,axis=1)
print('end')